package com.cwvs.cat.fragment;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.cwvs.cat.Applications;
import com.cwvs.cat.R;
import com.cwvs.cat.adapter.ActiveListAdapter;
import com.cwvs.cat.bean.ActiveContent;
import com.cwvs.cat.net.Port;

public class PlayContentFragment extends Fragment {

	private Activity mContext;;
	private ListView lv_active_list;
	private ActiveListAdapter activeListAdapter;
	private List<ActiveContent> list_active;
	public PlayContentFragment() {
		// TODO Auto-generated constructor stub
	}

	private void setList() {
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = getActivity();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		search();
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentLayout = inflater.inflate(R.layout.fragment_active_content,
				container, false);
		list_active = new ArrayList<ActiveContent>();

		initView(contentLayout);
		return contentLayout;
	}

	private void initView(View view) {
		lv_active_list = (ListView) view
				.findViewById(R.id.lv_active_list);
		initListView();
	}

	private void initListView() {

		setList();

	}

	private void search(){
		AjaxParams params = new AjaxParams();
		params.put("words","1");
		params.put("category","1");
		params.put("token",Applications.user.token);
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.actSerach,params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				System.out.println(t.toString());
				JSONObject json;
				try {
					JSONObject obj = null;
					JSONArray array=null,array2=null,arr = null;
					json = new JSONObject(t.toString());
					int code = json.getInt("code");
					if (code==0) {
						obj=json.getJSONObject("list");
						array2=obj.getJSONArray("header");
						for (int i = 0; i < array2.length(); i++) {
							String cate2 = (String) array2.get(i);
						//	list2.add(cate2 );
						}
						Applications.list_active3.clear();
						array=obj.getJSONArray("data");
						if (array.length()>0) {
							JSONArray jsonArray = array.getJSONArray(0);
							JSONObject jsonObject = jsonArray.getJSONObject(1);
							JSONObject listJsonObject = jsonObject.getJSONObject("list");
							JSONArray dataArray = listJsonObject.getJSONArray("data");
							for (int i = 0; i < dataArray.length(); i++) {
								arr = (JSONArray) dataArray.get(i);
								List<String>	list1 = new ArrayList<String>();
								for (int k = 0; k < arr.length(); k++) {
									String cate1 =arr.get(k).toString();
									list1.add(cate1);
								}
								ActiveContent activeContent = new ActiveContent(list1);
								Applications.list_active3.add(activeContent);
							}
						
						}
						activeListAdapter = new ActiveListAdapter(getActivity(),Applications.list_active3);
						lv_active_list.setAdapter(activeListAdapter);
					}else {
						Toast.makeText(getActivity(),json.getString("msg"),2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

}
